/**
 * Generali Solutions d'assurances - Tous droits réservés &copy; 2007 - 2010
 */
package fr.generali.accueilclient.opt.recherchemulticriteres.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import fr.generali.accueilclient.commun.exception.AccueilClientBusinessException;
import fr.generali.accueilclient.opt.recherchemulticriteres.om.ICriteresCampagneRechMultiCriteres;
import fr.generali.accueilclient.opt.recherchemulticriteres.om.ICriteresClientListeRechMultiCriteres;
import fr.generali.accueilclient.opt.recherchemulticriteres.om.ICriteresClientRechMultiCriteres;
import fr.generali.accueilclient.opt.recherchemulticriteres.om.IDossierRechMultiCriteres;
import fr.generali.accueilclient.opt.recherchemulticriteres.services.IRechercheListeDossiersMultiCriteresService;
import fr.generali.relationclient.marketing.opt.recherchemulticriteres.DtoDossier;
import fr.generali.relationclient.marketing.opt.recherchemulticriteres.services.IRechercheMultiCriteresWebService;
import fr.generali.relationclient.marketing.opt.recherchemulticriteres.services.RechercheWSBusinessException;
import fr.generali.relationclient.marketing.opt.recherchemulticriteres.services.RechercheWSTechnicalException;

/**
 * @author Holmes Kelly
 */

@Service("rechercheListeDossiersMultiCriteresService")
public class RechercheListeDossiersMultiCriteresService implements IRechercheListeDossiersMultiCriteresService {

    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(RechercheListeDossiersMultiCriteresService.class);

    /**
     * Appel mapper contenant les assembler
     */
    @Autowired
    @Qualifier("rechercheListeDossiersMultiCriteresMapper")
    private RechercheListeDossiersMultiCriteresMapper rechercheListeDossiersMultiCriteresMapper;

    /**
     * Appel web service RechercheMultiCriteresWebService
     */
    @Autowired
    @Qualifier("rechercheMultiCriteresWebService")
    private IRechercheMultiCriteresWebService rechercheMultiCriteresWebService;

    /**
     * {@inheritDoc}
     */
    public List<IDossierRechMultiCriteres> rechercherListeDossierCampagne(
                    ICriteresCampagneRechMultiCriteres criteresCampagne) throws AccueilClientBusinessException {

        List<IDossierRechMultiCriteres> listDTODossier = null;

        try {

            final List<DtoDossier> listerDossierCampagne =
                            rechercheMultiCriteresWebService
                                            .listerDossierCampagne(rechercheListeDossiersMultiCriteresMapper
                                                            .mapperCriteresCampagne2DtoCriteresCampagne(criteresCampagne));

            if (CollectionUtils.isNotEmpty(listerDossierCampagne)) {

                listDTODossier =
                                rechercheListeDossiersMultiCriteresMapper
                                                .mapperDtoDossier2DossierRechMultiCriteres(listerDossierCampagne);

            }

        } catch (RechercheWSBusinessException e) {

            LOGGER.debug(e.getMessage());

            throw new AccueilClientBusinessException(e.getMessage(), e);

        } catch (RechercheWSTechnicalException e) {

            LOGGER.debug(e.getMessage());

            throw new AccueilClientBusinessException(e.getMessage(), e);
        }

        return listDTODossier;
    }

    /**
     * {@inheritDoc}
     */
    public List<IDossierRechMultiCriteres> rechercherListeDossiersClient(ICriteresClientRechMultiCriteres criteresClient)
                    throws AccueilClientBusinessException {

        List<IDossierRechMultiCriteres> listDTODossier = null;

        if (criteresClient != null) {

            try {

                final List<DtoDossier> listerDossierClient =
                                rechercheMultiCriteresWebService
                                                .listerDossierClient(rechercheListeDossiersMultiCriteresMapper
                                                                .mapperCriteresClient2DtoCriteresClient(criteresClient));

                if (CollectionUtils.isNotEmpty(listerDossierClient)) {

                    listDTODossier =
                                    rechercheListeDossiersMultiCriteresMapper
                                                    .mapperDtoDossier2DossierRechMultiCriteres(listerDossierClient);

                }

            } catch (RechercheWSBusinessException e) {

                LOGGER.debug(e.getMessage());

                throw new AccueilClientBusinessException(e.getMessage(), e);

            } catch (RechercheWSTechnicalException e) {

                LOGGER.debug(e.getMessage());

                throw new AccueilClientBusinessException(e.getMessage(), e);

            }

        }

        return listDTODossier;
    }

    /**
     * {@inheritDoc}
     */
    public List<IDossierRechMultiCriteres> rechercherListeDossierParNumeroClient(
                    ICriteresClientListeRechMultiCriteres criteresClientListeRechMultiCriteres)
                    throws AccueilClientBusinessException {

        List<IDossierRechMultiCriteres> listDTODossier = new ArrayList<IDossierRechMultiCriteres>();

        try {
            final List<DtoDossier> listerDossierParNumeroClient =
                            rechercheMultiCriteresWebService
                                            .listerDossierByClientId(rechercheListeDossiersMultiCriteresMapper
                                                            .mapperCriteresClientId2DtoCriteresClientId(criteresClientListeRechMultiCriteres));

            if (CollectionUtils.isNotEmpty(listerDossierParNumeroClient)) {

                listDTODossier =
                                rechercheListeDossiersMultiCriteresMapper
                                                .mapperDtoDossier2DossierRechMultiCriteres(listerDossierParNumeroClient);
            }

        } catch (RechercheWSBusinessException e) {
            LOGGER.debug(e.getMessage());

            throw new AccueilClientBusinessException(e.getMessage(), e);
        } catch (RechercheWSTechnicalException e) {

            LOGGER.debug(e.getMessage());
        }

        return listDTODossier;
    }

}
